package com.book.servlet.page;

import com.book.service.StudentService;
import com.book.service.UserService;
import com.book.service.impl.StudentServiceImpl;
import com.book.service.impl.UserServiceImpl;
import com.book.util.ThymeleafUtil;
import org.thymeleaf.context.Context;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 **@Author:youpeng
 **Created on 2022/4/25
 */
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {

    UserService userService=new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Context context = new Context();
        String msg = (String) req.getSession().getAttribute("msg");
        context.setVariable("msg",msg);
        ThymeleafUtil.process("register.html",context, resp.getWriter());
        req.getSession().removeAttribute("msg");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String nickname = req.getParameter("nickname");
        String password = req.getParameter("password");
        String pwd = req.getParameter("pwd");
        if (password.equals(pwd)){
            userService.addUser(username,nickname,password);
            resp.sendRedirect("login");
        }else {
            req.getSession().setAttribute("msg",new String());
            this.doGet(req,resp);
        }
    }
}
